﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Register.Model;
using Register.BLL;
using System.Web.Script.Serialization;
using Register.Command;
using System.Text;

namespace RegisterWeb.Manager.EvaluationManager
{
    /// <summary>
    /// EvaluationInfoManagerServer 的摘要说明
    /// </summary>
    public class EvaluationInfoManagerServer : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            String action = context.Request["action"].ToString();
            if (action.Equals("list"))
            {
                int row = int.Parse(context.Request["rows"].ToString());
                int page = int.Parse(context.Request["page"].ToString());
                List<Register.Model.EvaluationInfo> list = new List<Register.Model.EvaluationInfo>();
                list = EvaluationInfoBLL.GetPagedEvaluationInfo(row, page, " EvaluationInfo_Status=1 ");

                List<EvaluationInfoJSON> json = new List<EvaluationInfoJSON>();
                foreach (Register.Model.EvaluationInfo evaluationInfo in list)
                {
                    json.Add(new EvaluationInfoJSON(evaluationInfo.EvaluationInfo_ID, evaluationInfo.Doctor_ID, evaluationInfo.PatientUserInfo_ID, getEvalWay(evaluationInfo.EvaluationInfo_EvalWay), evaluationInfo.EvaluationInfo_EvalContent, evaluationInfo.EvaluationInfo_AddTime.ToString(),evaluationInfo.EvaluationInfo_Status));
                }

                EvaluationInfoListJSON evaluationInfoListJSON = new EvaluationInfoListJSON();
                evaluationInfoListJSON.total = EvaluationInfoBLL.GetEvaluationInfoCount(null);
                evaluationInfoListJSON.rows = json;

                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(evaluationInfoListJSON));
            }
            else if (action.Equals("search"))
            {
                string EvalWay = context.Request["EvalWay"];
                string Begin = context.Request["Begin"];
                string End = context.Request["End"];
                EvaluationInfoListJSON evaluationInfoListJSON = new EvaluationInfoListJSON();
                evaluationInfoListJSON.rows = new List<EvaluationInfoJSON>();

                int row = int.Parse(context.Request["rows"].ToString());
                int page = int.Parse(context.Request["page"].ToString());

                StringBuilder strBuilder = new StringBuilder();
                strBuilder.Append(" EvaluationInfo_Status='1' ");
                if (!String.IsNullOrEmpty(EvalWay))
                {
                    strBuilder.Append(" and  EvaluationInfo_EvalWay='").Append(EvalWay).Append("' ");
                }
                if (!(String.IsNullOrEmpty(Begin) || String.IsNullOrEmpty(End)))
                {
                    strBuilder.Append(" and EvaluationInfo_AddTime  Between '").Append(Begin).Append("' and '").Append(End).Append("'");
                }
                List<Register.Model.EvaluationInfo> list = EvaluationInfoBLL.GetPagedEvaluationInfo(row, page, strBuilder.ToString());
                evaluationInfoListJSON.total = EvaluationInfoBLL.GetEvaluationInfoCount(strBuilder.ToString());


                foreach (Register.Model.EvaluationInfo evaluationInfo in list)
                {
                    evaluationInfoListJSON.rows.Add(new EvaluationInfoJSON(evaluationInfo.EvaluationInfo_ID, evaluationInfo.Doctor_ID, evaluationInfo.PatientUserInfo_ID, getEvalWay(evaluationInfo.EvaluationInfo_EvalWay), evaluationInfo.EvaluationInfo_EvalContent, evaluationInfo.EvaluationInfo_AddTime.ToString(), evaluationInfo.EvaluationInfo_Status));
                }


                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(evaluationInfoListJSON));
            }
            else if (action.Equals("EvalWay"))
            {
                List<Parameter> list;
                JavaScriptSerializer jss = new JavaScriptSerializer();

                list = ParamterHelper.GetParametersByType("regWay");
                context.Response.Write(jss.Serialize(list));
            }
        }
        public String getEvalWay(String EvalWay)
        {
            String TextEvalWay = "";
            List<Parameter> list;
            JavaScriptSerializer jss = new JavaScriptSerializer();

            list = ParamterHelper.GetParametersByType("regWay");
            foreach (Parameter parameter in list)
            {
                if (parameter.SystemParameter_ID == EvalWay)
                    TextEvalWay = parameter.SystemParameter_Name;
            }
            return TextEvalWay;
        }
        class message
        {
            public message(bool success, string msg)
            {
                this.msg = msg;
                this.success = success;
            }
            public bool success { get; set; }
            public string msg { get; set; }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        class EvaluationInfoListJSON
        {

            public int total { get; set; }
            public List<EvaluationInfoJSON> rows { get; set; }

        }

        class EvaluationInfoJSON
        {
            public String ID { get; set; }
            public String Doctor_ID { get; set; }
            public String PatientUserInfo_ID { get; set; }
            public String EvalWay { get; set; }
            public String EvalContent { get; set; }
            public String AddTime { get; set; }
            public String Status { get; set; }

            public EvaluationInfoJSON(String ID, String Doctor_ID, String PatientUserInfo_ID, String EvalWay,String EvalContent, String AddTime, String Status )
            {
                this.ID = ID;
                this.Doctor_ID = Doctor_ID;
                this.PatientUserInfo_ID = PatientUserInfo_ID;
                this.EvalWay = EvalWay;
                this.EvalContent = EvalContent;
                this.AddTime = AddTime;
                this.Status = Status;
            }

        }
    }
}